<?php

class SiteAdmin extends AdminBase
{
	private $nohf = ['configmainupdate', 'configseoupdate', 'addmenu', 'menudoadd',  'menudel'];
	private $mysql = null;

	public function __construct()
	{
		parent::__construct();
		if(!in_array(N_ACTION,$this->nohf)){
			n_mod('skin')->header('admin');
		}
		$this->mysql = n_api('mysql')->cname('weixin');
		// $this->api = n_api('user');
	}

	public function siteConfig()
	{
		$data = $this->mysql->fetchFirst('select * from site_main_config');
		$this->_html('siteConfig', $data);
	}

	public function configMainUpdate()
	{
		$param = $this->param('
			siteName:char:ignore,
			siteSecondName:char:ignore,
			siteUrl:char:ignore,
			record:char:ignore,
			siteEmail:char:ignore,
			copyright:char:ignore
		');

		$data = [
			'site_name' => $param['siteName'],
			'site_second_name' => $param['siteSecondName'],
			'site_url' => $param['siteUrl'],
			'record' => $param['record'],
			'site_email' => $param['siteEmail'],
			'copyright' => $param['copyright']
		];

		$ppid = $_SESSION['ppid'];
		$this->mysql->update('site_main_config', $data, "id=1");
		echo "ok";
	}

	public function seoConfig()
	{
		$data = $this->mysql->fetchFirst('select * from site_seo_config');
		$this->_html('seoConfig', $data);
	}

	public function configSeoUpdate()
	{
		$param = $this->param('
			seoTitle:char:ignore,
			seoKeyword:char:ignore,
			seoDesc:char:ignore
		');

		$data = [
			'seo_title' => $param['seoTitle'],
			'seo_keyword' => $param['seoKeyword'],
			'seo_desc' => $param['seoDesc']
		];

		$this->mysql->update('site_seo_config', $data, "id=1");
		echo "ok";
	}

	public function menuCategory()
	{
		$data = $this->mysql->select("select * from site_menu_category where isdel = 'N'");
		$this->_html('menuCategory', $data);
	}

	public function menuCategoryManage()
	{
		$param = $this->param('cid:char:ignore');
		
		if (isset($param['cid']))
			$cid = $param['cid'];
		else
			$cid = 1;

		$categories = $this->mysql->select("select * from site_menu_category where isdel = 'N'");
		$data['detail'] = $this->mysql->select("select * from site_menu_detail where cid = $cid and isdel = 'N' order by dorder desc");
		foreach ($categories as $value) {
			if ($value['id'] == $cid)
				$data['category'] = $value;
		}
		$data['categories'] = $categories;

		$this->_html('menuCategoryManage', $data);
	}

	public function addMenu()
	{
		$data['categories'] = $this->mysql->select("select * from site_menu_category where isdel = 'N'");
		$this->_html('addMenu', $data);
	}

	public function menuDoAdd()
	{
		$param = $this->param('
			menuName:char:empty,
			menuUrl:char:empty,
			blank:char:empty,
			status:char:empty,
			dorder:char:empty,
			cid:char:empty
		');

		$data = [
			'cid' => $param['cid'],
			'dorder' => $param['dorder'],
			'name' => $param['menuName'],
			'blank' => $param['blank'],
			'status' => $param['status'],
			'url' => $param['menuUrl'],
			'ctime' => time()
		];

		$this->mysql->insert('site_menu_detail', $data);
		echo "ok";
	}

	public function menuEdit()
	{
		$param = $this->param('
			cid:char:empty,
			did:char:empty'
		);

		$cid = $param['cid'];
		$did = $param['did'];

		$data['category'] = $this->mysql->fetchFirst("select * from site_menu_category where id = $cid and isdel = 'N'");
		$data['detail'] = $this->mysql->fetchFirst("select * from site_menu_detail where cid = $cid and id = $did and isdel = 'N'");

		$this->_html('menuEdit', $data);
	}

	public function menuDoUpdate()
	{
		$param = $this->param('
			menuName:char:empty,
			menuUrl:char:empty,
			blank:char:empty,
			status:char:empty,
			dorder:char:empty,
			cid:char:empty,
			did:char:empty
		');

		$cid = $param['cid'];
		$did = $param['did'];

		$data = [
			'dorder' => $param['dorder'],
			'name' => $param['menuName'],
			'blank' => $param['blank'],
			'status' => $param['status'],
			'url' => $param['menuUrl'],
			'ctime' => time()
		];

		$where = "id = $did and cid = $cid";
		$this->mysql->update('site_menu_detail', $data, $where);
		n_mod("skin")->save("保存成功",3);
	}

	public function menuDel()
	{
		$param = $this->param('
			cid:char:empty,
			did:char:empty'
		);

		$cid = $param['cid'];
		$did = $param['did'];

		$this->mysql->del('site_menu_detail', $did);
		echo "ok";
	}

	public function __destruct(){
		//加入标准尾。
		if(!in_array(N_ACTION,$this->nohf) && 'page'==N_TYPE){
			n_mod('skin')->footer();
		}
	}
}